//
//  AppDelegate.m
//  TLKeepCredit
//
//  Created by mac on 2017/9/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "TLLoginVC.h"
#import "TLRegistFindVC.h"
#import "TLTabbarVC.h"

#import <UIKit/UIKit.h>
@interface TLLoginVC ()


@property (weak, nonatomic) IBOutlet UITextField *tfUserName;
@property (weak, nonatomic) IBOutlet UITextField *tfPwd;

@property (weak, nonatomic) IBOutlet UIView *loginView;
@property (weak, nonatomic) IBOutlet TLBaseButton *btnLogin;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutCenterQQ;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutCenterWX;

@property (weak, nonatomic) IBOutlet UIButton *btnWX;
@property (weak, nonatomic) IBOutlet UIButton *btnQQ;


@property (weak, nonatomic) IBOutlet UIView *viewLoginSelect;

@end

@implementation TLLoginVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *userName = [kNSUserDefaults objectForKey:kUserName];
    if (userName) {
        _tfUserName.text = userName;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self initData];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - 初始化
#pragma mark -
#pragma mark ---初始化UI
-(void)initUI {
    self.title = @"登陆";
    if(_popToVC) {
        [self setLeftCustomerItem];
    }
//    [self isOpenQQorWX];
    
    _loginView.layer.cornerRadius = 2;
    _loginView.layer.masksToBounds = YES;
    _loginView.layer.borderWidth = 1;
    _loginView.layer.borderColor = UIColorFormRGB_line_E8.CGColor;
    [_btnLogin setBackgroundColor:UIColorFormRGB_theme];
    
    
    //设置背景img
//    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"home_screen_fiv"]];
//    [self.view setBackgroundColor:bgColor];
    
}
#pragma mark ---初始化数据
-(void)initData {
//    NSString *userName =[kNSUserDefaults objectForKey:kUserName];
//    if(userName) {
//        _tfUserName.text = userName;
//    }
}

-(void)isOpenQQorWX {
    BOOL qq = [[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"mqqapi://"]];
    BOOL weichat    = [[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"weixin://"]];
    
    if (!weichat && !qq) {
        _viewLoginSelect.hidden = YES;
        return;
    }
    _viewLoginSelect.hidden = NO;
    if (weichat && !qq) {
        _layoutCenterWX.constant = 0;
        _btnQQ.hidden = YES;
        _btnWX.hidden = NO;
        return;
    }
    if (!weichat && qq) {
        _layoutCenterQQ.constant = 0;
        _btnQQ.hidden = NO;
        _btnWX.hidden = YES;
        return;
    }
}


#pragma mark - 请求 事件
#pragma mark -

+(void)autoLogin:(void (^)(BOOL isSuc))end {
    NSString *userName = [kNSUserDefaults objectForKey:kUserName];
    NSString *userPwd = [kNSUserDefaults objectForKey:kUserPwd];
    if (userName && userPwd) {
        // 将请求参数放在请求的字典里
        NSDictionary *param = @{@"username":userName,
                                @"password":userPwd};
        [BaseRequest POST:kUrl_login parameters:param success:^(id responseObject, int code) {
            ResLoanKou *res = [ResLoanKou modelWithDict:responseObject];
            if (res.status == 1) {
//                ResLogin *res = [ResLogin modelWithDict:responseObject];
//                [TLUserData loginSucWithModel:res.data];
            }
            if (end) {
                end(YES);
            }
        } failure:^(NSError *error) {
            if (end) {
                end(NO);
            }
        }];
    }
}

#pragma mark ---事件 登陆
-(void)getReqForLogin {
    if (_popToVC) {
        if (_loginSuc) {
            _loginSuc();
        }
        [self.navigationController popToViewController:_popToVC animated:YES];
    }
    
//    [self.view showHUDHoldOn];
//    // 将请求参数放在请求的字典里
//    NSDictionary *param = @{@"username":_tfUserName.text,
//                            @"password":_tfPwd.text};
//    [BaseRequest POST:kUrl_login parameters:param success:^(id responseObject, int code) {
//        ResLoanKou *res = [ResLoanKou modelWithDict:responseObject];
//        if (res.status == 1) {
//            [self.view dismissHUD];
//            ResLogin *res = [ResLogin modelWithDict:responseObject];
//            [TLUserData loginSucWithModel:res.data];
//            [kNSUserDefaults setObject:_tfUserName.text forKey:kUserName];
//            [kNSUserDefaults setObject:_tfPwd.text forKey:kUserPwd];
//
//            if (_popToVC) {
//                if (_loginSuc) {
//                    _loginSuc();
//                }
//                [self.navigationController popToViewController:_popToVC animated:YES];
//            }
//            return ;
//        }
//        [self.view dismissHUDWithError:res.msg];
//    } failure:^(NSError *error) {
//        [self.view dismissHUDWithErrorDefult];
//    }];
}

#pragma mark ---btn 登陆
- (IBAction)btnLogin:(id)sender {
    
    [_tfUserName resignFirstResponder];
    [_tfPwd resignFirstResponder];
    if(_tfUserName.text.length<=0 || _tfPwd.text.length<=0 ) {
        [self.view showMessage:@"请输入账号密码!"];
        return;
    }
    [self getReqForLogin];
}

- (IBAction)btnRetrieveAction:(id)sender {
    [self navigationPushViewController:[TLRegistFindVC class]  animation:YES withOperation:^(id vcInstance) {
        TLRegistFindVC *vc= vcInstance;
        vc.title = @"找回密码";
        vc.popToVC = self;
//        [vc setBtnPopToVC:^{
//            _tfUserName.text = [kNSUserDefaults objectForKey:kUserName];
//        }];
    }];
    
}
- (IBAction)btnRegisterAction:(id)sender {
    
    [self navigationPushViewController:[TLRegistFindVC class]  animation:YES withOperation:^(id vcInstance) {
        TLRegistFindVC *vc= vcInstance;
        vc.title = @"注册";
        vc.popToVC = self;
        vc.isRegister = YES;
//        [vc setBtnPopToVC:^{
//            _tfUserName.text = [kNSUserDefaults objectForKey:kUserName];
//        }];
    }];
}

- (IBAction)btnWxLogin:(id)sender {
//    [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
}

- (IBAction)btnQQLogin:(id)sender {
//    [self getUserInfoForPlatform:UMSocialPlatformType_QQ];
}

#pragma mark ---获取授权
//- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
//{
//    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
//        if (error) {
//            
//        } else {
//            UMSocialUserInfoResponse *resp = result;
//            
//            [self getReqForLoginOpenid:resp.openid Name:resp.name Type:platformType];
//        }
//    }];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
